using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.IO;

/// <summary>
/// Summary description for Class1
/// </summary>
public class HttpRequestResponse
{
    private string URI;
    private string Request;
    private string UserName;
    private string UserPwd;
    private string ProxyServer;
    private int ProxyPort;
    private string RequestMethod = "GET";

    public HttpRequestResponse(string pRequest, string pURI)
	{
		//
		// TODO: Add constructor logic here
		//
        Request = pRequest;
        URI = pURI;
	}

    public string HTTP_USER_NAME
    {
        get
        {
            return UserName;
        }
        set
        {
            UserName = value;
        }
    }

    public string HTTP_USER_PASSWORD
    {
        get
        {
            return UserPwd;
        }
        set
        {
            UserPwd = value;
        }
    }

    public string PROXY_SERVER
    {
        get
        {
            return ProxyServer;
        }
        set
        {
            ProxyServer = value;
        }
    }

    public int PROXY_PORT
    {
        get
        {
            return ProxyPort;
        }
        set
        {
            ProxyPort = value;
        }
    }

    public string SendRequest()
    {
        string FinalResponse = "";
        string Cookie = "";

        NameValueCollection collHeader = new NameValueCollection();

        HttpWebResponse webresponse;

        HttpBaseClass BaseHttp = new HttpBaseClass(UserName, UserPwd, ProxyServer, ProxyPort, Request);

        try
        {
            HttpWebRequest webrequest = BaseHttp.CreateWebRequest(URI, collHeader, RequestMethod, true);
            webresponse = (HttpWebResponse)webrequest.GetResponse();

            string ReUri = BaseHttp.GetRedirectURL(webresponse, ref Cookie);
            webresponse.Close();
            ReUri = ReUri.Trim();

            if (ReUri.Length == 0)
            {
                ReUri = URI;
            }
            RequestMethod = "POST";
            FinalResponse = BaseHttp.GetFinalResponse(ReUri, Cookie, RequestMethod, true);
        }
        catch (WebException ex)
        {
            throw CatchHttpException(FinalResponse = ex.Message);
        }
        catch (System.Exception ex)
        {
            throw new Exception(FinalResponse = ex.Message);
        }
        finally
        {
            BaseHttp = null;
        }
        
        return FinalResponse;
    }

    private WebException CatchHttpException(string ErrMsg)
    {
        ErrMsg = "Error During Wen Interface. Error is:" + ErrMsg;
        return new WebException(ErrMsg);
    }
}
